package Algorithm.dynamic.palindrome;

/**
 * @author liujun
 * @version 1.0
 * @date 2019-11-07 22:42
 * @author—Email ljfirst@mail.ustc.edu.cn
 * @description
 */
public class FindPalindromeOps extends Palindrome{

    int left = 0;
    int maxLength = 0;

    public String FindPalindrome(String s) {
        if (s == null || s.length() == 0) return "";

        for (int i = 0; i < s.length(); i++) {
            helper(s, i, i);
            helper(s, i, i + 1);
        }
        return s.substring(left, left + maxLength);
    }

    public void helper(String s, int l, int r) {
        while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
            if (r - l + 1 > maxLength) {
                maxLength = r - l + 1;
                left = l;
            }
            l--;
            r++;
        }
    }
}
